package com.elinkway.infinitemovies.dao;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.elinkway.infinitemovies.application.MoviesApplication;
import com.elinkway.infinitemovies.utils.Utils;

import static android.databinding.repacked.kotlin.text.Typography.times;

public class PreferencesManager {

    private Context context;
    private static final String SIFTDATA = "siftdata";
    private static final String FIRSTLAUNCH = "firstlaunch";
    private static final String IP = "ip";
    private static final String REPORT = "report";
    private static final String FIRST_ENTER = "first_time_enter";
    private static final String SPEED = "speed";
    private static final String SPEED_LIMIT = "speed_limit";
    private static final String INTERVAL = "interval";
    private static final String SPLASHINTERVAL = "splashInterval";
    private static final String SPLASH = "splash";
    private static final String SPLASHILIMIT = "splashilimit";
    private static final String SPLASH_GDT_SHOWTIMES = "splash_gdt_showtimes";
    private static final String SPLASH_GDT_MS= "splash_gdt_ms";
    private static final String APP_LIST = "app_list";
    private static final String LAST_UPLOAD_APP_LIST_TIME = "last_upload_app_list_time";
    private static final String TAGS = "app_upload_tags";


    private PreferencesManager(Context context) {
        this.context = context;
    }

    private static PreferencesManager instance = new PreferencesManager(MoviesApplication.getInstance());

    public static PreferencesManager getInstance() {
        return instance;
    }

    public void saveIsFirstTimeEnter(boolean isFirstTimeEnter) {
        SharedPreferences sp = context.getSharedPreferences(FIRST_ENTER, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putBoolean(FIRST_ENTER, isFirstTimeEnter);
        editor.commit();
    }

    public boolean isFirstTimeEnter() {
        SharedPreferences sp = context.getSharedPreferences(FIRST_ENTER, Context.MODE_PRIVATE);
        boolean isFirstTime = sp.getBoolean(FIRST_ENTER, true);
        return isFirstTime;
    }

    public void savePublicIp(String ip) {
        SharedPreferences sp = context.getSharedPreferences(IP, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString("publicip", ip);
        editor.commit();
    }

    public String getPublicIp() {
        SharedPreferences sp = context.getSharedPreferences(IP, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        String publicIp = sp.getString("publicip", Utils.getLocalIpAddress());
        return publicIp;
    }

    public void saveIsReport(String report) {
        SharedPreferences sp = context.getSharedPreferences(REPORT, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(REPORT, report);
        editor.commit();
    }

    public String getReport() {
        SharedPreferences sp = context.getSharedPreferences(REPORT, Context.MODE_PRIVATE);
        String report = sp.getString(REPORT, "0");
        return report;
    }

    public boolean isFirstLaunch() {
        SharedPreferences sp = context.getSharedPreferences(FIRSTLAUNCH, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        boolean firstlaunch = sp.getBoolean("firstlaunch", true);
        if (firstlaunch) {
            editor.putBoolean("firstlaunch", false);
            editor.commit();
            return true;
        } else {
            return false;
        }
    }

    public void setSpeed(int speed) {
        SharedPreferences sp = context.getSharedPreferences(SPEED, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(SPEED_LIMIT, speed);
        editor.commit();
    }

    public int getSpeed() {
        SharedPreferences sp = context.getSharedPreferences(SPEED, Context.MODE_PRIVATE);
        return sp.getInt(SPEED_LIMIT, -1);
    }

    public void setSplashInterval(int interval) {
        SharedPreferences sp = context.getSharedPreferences(INTERVAL, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(SPLASHINTERVAL, interval);
        editor.commit();
    }

    public int getSplashInterval() {
        SharedPreferences sp = context.getSharedPreferences(INTERVAL, Context.MODE_PRIVATE);
        return sp.getInt(SPLASHINTERVAL, -1);
    }

    public void setLastUpdateAppListTime(long time) {
        SharedPreferences sp = context.getSharedPreferences(APP_LIST, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putLong(LAST_UPLOAD_APP_LIST_TIME, time);
        editor.commit();
    }

    public long getLastUpdateAppListTime() {
        SharedPreferences sp = context.getSharedPreferences(APP_LIST, Context.MODE_PRIVATE);
        return sp.getLong(LAST_UPLOAD_APP_LIST_TIME, 0L);
    }

    public void saveTags(String tags) {
        SharedPreferences sp = context.getSharedPreferences(TAGS, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putString(TAGS, tags);
        editor.commit();
    }

    public String getTags() {
        SharedPreferences sp = context.getSharedPreferences(TAGS, Context.MODE_PRIVATE);
        String report = sp.getString(TAGS, "");
        return report;
    }

    public void setSplashLimit(int limit) {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(SPLASHILIMIT, limit);
        editor.commit();
    }

    public int getSplashLimit() {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        return sp.getInt(SPLASHILIMIT, Integer.MAX_VALUE);
    }

    public void setSplashGdtShowtimes(int times) {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putInt(SPLASH_GDT_SHOWTIMES, times);
        editor.commit();
    }

    public int getSplashGdtShowtimes() {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        return sp.getInt(SPLASH_GDT_SHOWTIMES, 0);
    }

    public void setSplashGdtMs(long ms) {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        Editor editor = sp.edit();
        editor.putLong(SPLASH_GDT_MS, ms);
        editor.commit();
    }

    public long getSplashGdtMs() {
        SharedPreferences sp = context.getSharedPreferences(SPLASH, Context.MODE_PRIVATE);
        return sp.getLong(SPLASH_GDT_MS, 0);
    }
}
